var app = getApp();
var WxParse = require('../../lib/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../utils/user.js');

Page({
    data: {
        id: 0,
        goodsInfo: {},
        noCollectImage: '/static/images/icon_like.png',
        hasCollectImage: '/static/images/icon_like_checked.png',
        collectImage: '/static/images/icon_like.png',
        amoy: {}
    },


    // 获取商品信息
    getGoodsInfo: function (id) {
        console.log("id")
        console.log(id)
        console.log("id")
        let that = this;
        let goodsList = wx.getStorageSync("goodsList");
        let hotList = wx.getStorageSync("hotGoods");

        if (goodsList !== null && goodsList.length !== 0) {
            goodsList.forEach((res) => {
                if (id == res.itemId) {
                    this.setData({
                        goodsInfo: res
                    })
                    return false;
                }
            });
        }

        if (hotList !== null && hotList.length !== 0) {
            hotList.forEach((res) => {
                if (id == res.itemId) {
                    that.setData({
                        goodsInfo: res
                    })
                    return false;
                }
            });
        }


    },

    // 获取推荐商品
    getGoodsRelated: function () {
        let that = this;
        util.request(api.GoodsRelated, {
            id: that.data.id
        }).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    relatedGoods: res.data.goodsList,
                });
            }
        });
    },

    //  页面加载时显示
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        this.getGoodsInfo(options.id);
    },

    // 购买弹出淘口令
    handleBuy() {
        let pictUrl = this.data.goodsInfo.pictUrl;
        let couponShareUrl = this.data.goodsInfo.couponShareUrl;
        this.setData({
            amoy: {
                pictUrl: pictUrl,
                couponShareUrl: couponShareUrl
            }

        })

        wx.setStorageSync("amoy", this.data.amoy);

        wx.navigateTo({
            url: "/pages/taokouling/taokouling"
        })
    },

    //页面推荐理由复制
    handleCopy() {
        let that = this;
        console.log(that.data.goodsInfo.itemDescription);
        wx.setClipboardData({
            data: that.data.goodsInfo.itemDescription,
            success: res => {
                console.log(res);
            }
        })
    },

    // 下拉刷新
    onPullDownRefresh() {
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.getGoodsInfo();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },


})
